Skip to content

Compile in-process on OCaml domains in OCaml rewatch - #8781

Merged
cknitt merged 26 commits into
rewatch-ocaml-portfrom
rewatch-ocaml-in-process
Oct 15, 2026
Merged

cknitt merged 26 commits into
rewatch-ocaml-portfrom
rewatch-ocaml-in-process

Conversation

@cknitt

@cknitt cknitt commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

Stacked on #8780.

This PR moves compilation into the OCaml rewatch process. Instead of starting bsc for every parse and compile, rescript links the compiler and runs requests on a bounded pool of OCaml 5 domains. That removes process start-up and pipe handling, which make the subprocess port slower than Rust on clean builds (see Performance below). It also opens the door to sharing compiler state between requests later. The compiler side, which makes requests safe to run repeatedly and in parallel and captures their output, is #8864.

Changes

  • Rewatch schedules independent parse and compile requests, and namespace maps, on worker domains. By default that's at most 8, and one fewer than the CPU count; REWATCH_COMPILER_DOMAINS overrides it, and a count above the machine's limit is lowered to it. The worker that compiled a module also publishes its artifacts; build-state updates stay on the scheduling domain. Workers get a 2M-word minor heap, because with the default one they keep stopping each other for minor collections (see Memory below). PPXs and hooks still run as external processes.
  • Compiler output is captured in memory, so builds need no temporary directory. Output written before an escaping exception is reported with the failed module.
  • A termination signal stops the build at the next poll point, after the modules compiling in-process finish, so a stop never leaves a half-written artifact. Repeated signals are ignored: the npm launcher forwards the Ctrl-C the terminal already delivered.
  • A damaged published AST, such as one cut short by a killed build, makes its module parse again instead of failing every later build.
  • rescript format also runs on the embedded compiler. PPXs, js-post-build, --after-build and the lock-owner check are the only remaining subprocesses. RESCRIPT_BSC_EXE now only affects rescript-rust, and CONTRIBUTING.md says how to test a local compiler instead.
  • The compiler's build identity is a digest of its sources, linked into the driver, so OCaml rewatch rebuilds after a compiler change without fingerprinting an installed executable. lib/bs/compiler-info.json still records the bsc.exe shipped next to rescript.exe; the VS Code extension reads bsc_path to find bsc.exe and the other platform binaries. A development build, which has no bsc.exe next to it, records the one in the installed @rescript/<platform> package.

Performance

Results

A real-world project with 1438 modules, on a MacBook Pro (Apple M1 Pro):

Clean build Edit one file
Rust rewatch 7.35 s about the same
This PR 4.00 s about the same

tests/tests (629 modules) on an idle Linux VM with 10 cores (arm64), median of 5 interleaved runs:

Clean build CPU Peak RSS No-op build Edit one file
Rust rewatch 1.59 s 11.5 s 317 MB 0.24 s 0.24 s
Subprocess port (#8780) 2.06 s 11.5 s 317 MB 0.18 s 0.19 s
This PR 0.88 s 2.3 s 640 MB 0.08 s 0.08 s

Clean builds are about 1.8x faster on both machines, with about 5x less CPU time, because no bsc process has to start for each file. Peak RSS is that of the largest single process: the in-process build keeps all compiler state in one process, whereas Rust rewatch spreads it over its bsc processes, whose combined memory was not measured. Builds after a one-file edit do little compiling, so they mostly cost fixed work such as package discovery and freshness checks.

Memory

  • Worker domains get a 2M-word (16 MB) minor heap instead of OCaml's default 256k words, which made clean builds about 15% faster. Larger heaps help a little more but cost memory: 8M words are 5% faster for 210 MB more, 16M words 10% faster for 365 MB more.
  • Peak RSS for tests/tests is about 410 MB with one domain and about 640 MB with the default 8.

Scaling with the domain count

tests/tests on the same VM:

Domains Clean build CPU Summed wall time of the compile requests
1 1.87 s 1.9 s 1.37 s
2 1.16 s 1.9 s 1.57 s
4 0.92 s 2.0 s 1.95 s
8 0.89 s 2.4 s 2.64 s

Builds scale well up to 4 domains; 8 add almost nothing. Two things limit them:

  • The end of the build. Once most modules are done, the build waits for the few slow ones that remain; with 8 domains, an average of 3.6 workers are busy. In tests/tests, one module dominates this because of slow compilation of its character range patterns (Character range patterns compile in quadratic time and memory #8866).
  • Shared minor collections. In OCaml 5, a minor collection stops all domains and collects all their minor heaps whenever any one of them is full. Requests are almost 2x slower at 8 domains than at 1, while CPU time barely grows: they wait instead of working. Runtime traces (olly) show workers spend about 25% of their time at the barrier that ends each collection, which waits for the domain with the most to promote. Because each heap is collected before its young data has time to die, the same 226M words of allocation promote 29M words to the major heap instead of 6M.

The machine is not the limit: four independent single-domain builds run side by side were only 1.13x slower than one alone. Outside compilation, the build spends about 0.16 s in stages that run on one domain (preparing the package tree, parsing, starting up).

On one domain, compiling splits into typing (48%), translating to Lambda (12%), optimizing Lambda and generating JavaScript (13%), loading .cmi files (8%, about 4 per module) and writing .cmt files (7%).

Follow-ups

  1. Worker processes instead of domains. Persistent single-domain processes running the same in-process requests each have their own GC, so the shared minor collections and their barrier wait go away. A crashing worker also no longer takes down the build. This would build on this PR, with a switch for comparing processes against domains.
  2. Start long modules first. The scheduler currently starts whichever module is ready. Using each module's compile time from the previous build, it could start the longest ones, and those that many others wait for, as early as possible, so the build doesn't end with idle workers waiting for one late, slow module.
  3. Allocate less. Every word allocated brings the next shared collection closer, and Gc.Memprof can show where typing allocates.
  4. Less repeated work per module: a per-worker .cmi cache validated by digest, keeping ASTs in memory between parsing and compiling, and writing .cmt files off the critical path.
  5. Faster incremental builds: keep loaded compiler state across watch-mode rebuilds.

ThreadSanitizer

An OCaml 5.5.0 ThreadSanitizer build of rescript reported no data races in compiler or rewatch code on clean builds of rewatch/testrepo (4 domains), tests/tests (2, 8 and 12 domains), the gentype example and the React PPX tests, or in the rewatch unit tests. Two reports appeared in some tests/tests runs, both inside the runtime's parallel minor GC (oldify_one against pool_allocate). With the shared Js_analyzer accumulator restored, the same tests/tests build reports 91 races, all in Js_analyzer, so the tool does catch this class of bug. This run predates publishing on the worker domains.

Tests

  • The focused suite uses a large module to keep an in-process compile running. With it, it tests:
    • that a second build waits for the build lock;
    • that repeated signals still stop cleanly;
    • that TERM, HUP and QUIT during compilation stop with the signal's status;
    • that removing watch.lock stops a watcher's compile;
    • that an atomic save during a rebuild is built.
  • It also tests:
    • that a damaged published AST is re-parsed;
    • that builds need no temporary directory;
    • that a failing PPX's stderr doesn't shift its error;
    • PPX temp files under a relative TMPDIR;
    • that a stop is noticed between namespace maps;
    • that a failed parse stays pending.
  • Unit tests check that an exception escaping the compiler fails only its own module and that PPX cancellation returns promptly.
  • rewatch-ocaml/bench compares clean-build artifacts across domain counts; it does not run in CI.

🤖 Generated with Claude Code

@cknitt
cknitt added this pull request to stack #8782 October 10, 2026 16:36
@codecov

codecov Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.94%. Comparing base (002d5dd) to head (6d0571e).

Additional details and impacted files
@@                 Coverage Diff                 @@
##           rewatch-ocaml-port    #8781   +/-   ##
===================================================
  Coverage               80.94%   80.94%           
===================================================
  Files                     453      453           
  Lines                   62839    62839           
===================================================
  Hits                    50865    50865           
  Misses                  11974    11974           
Files with missing lines Coverage Δ
compiler/bsc/rescript_compiler_driver.ml 81.22% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@cknitt
cknitt force-pushed the rewatch-ocaml-in-process branch 2 times, most recently from 236cf9b to 3de8d08 Compare October 10, 2026 18:59
@pkg-pr-new

pkg-pr-new Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@8781

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@8781

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@8781

@rescript/darwin-x64

npm i https://pkg.pr.new/@rescript/darwin-x64@8781

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@8781

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@8781

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@8781

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@8781

commit: 6d0571e

@cknitt
cknitt force-pushed the rewatch-ocaml-in-process branch from 0ba9249 to 6bcbfad Compare October 12, 2026 05:57
@cknitt
cknitt force-pushed the rewatch-ocaml-in-process branch from 6bcbfad to 6a72ee9 Compare October 12, 2026 06:08
@cknitt
cknitt force-pushed the rewatch-ocaml-in-process branch 3 times, most recently from 20777a1 to ea7a294 Compare October 12, 2026 22:27
@cknitt
cknitt force-pushed the rewatch-ocaml-in-process branch from ea7a294 to 5c7c670 Compare October 13, 2026 01:04
@cknitt
cknitt force-pushed the rewatch-ocaml-in-process branch from 5c7c670 to 4980a8d Compare October 13, 2026 13:48
@cknitt
cknitt force-pushed the rewatch-ocaml-in-process branch from 4980a8d to c05044a Compare October 13, 2026 20:45
@cknitt
cknitt force-pushed the rewatch-ocaml-in-process branch from eaae0c7 to 868272e Compare October 14, 2026 10:29
@cknitt
cknitt force-pushed the rewatch-ocaml-in-process branch from 868272e to 8a10d3d Compare October 14, 2026 15:51
@cknitt
cknitt removed this pull request from stack #8782 October 14, 2026 15:51
@cknitt
cknitt added this pull request to stack #8865 October 14, 2026 15:52
@cknitt cknitt mentioned this pull request Oct 14, 2026
@cknitt

cknitt commented Oct 14, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-14T18:17:14.994232Z d2d1c04 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 61da00ebaa

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread rewatch-ocaml/ast_header.ml
cknitt and others added 17 commits October 14, 2026 19:41
The first signal still waits for modules compiling in-process; a second one
releases the held build locks and exits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72
Signed-off-by: Christoph Knittel <ck@cca.io>
Threads may handle INT and TERM in either order, so the immediate exit reports 130 or 143. A two-second deadline still rules out the graceful stop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72
Signed-off-by: Christoph Knittel <ck@cca.io>
The cleanup also runs when the build is interrupted while a PPX runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72
Signed-off-by: Christoph Knittel <ck@cca.io>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72
Signed-off-by: Christoph Knittel <ck@cca.io>
The large module from the second-signal test keeps the first build compiling. That test now checks the real output name, Slow.js.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72
Signed-off-by: Christoph Knittel <ck@cca.io>
…rewatch

The npm launcher forwards the Ctrl-C the terminal already sent, so the second-signal exit cut every stop short and could truncate artifacts. A damaged published AST now makes its module parse again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72
Signed-off-by: Christoph Knittel <ck@cca.io>
…g failures

Capture compiler output in memory, write a PPX's stderr past the formatter, close artifact readers on failure, make PPX temp paths absolute, open .cmi files close-on-exec, and report a bad REWATCH_COMPILER_DOMAINS cleanly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72
Signed-off-by: Christoph Knittel <ck@cca.io>
They ran one at a time through Process.run_task, which ignored a stop request until the last map.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72
Signed-off-by: Christoph Knittel <ck@cca.io>
Three regression tests now fail with their bug reintroduced, the Windows backend is type-checked against platform.mli again, and the interrupt, atomic-save and retained-parse tests the test proxy removal dropped are back, using a large module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72
Signed-off-by: Christoph Knittel <ck@cca.io>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72
Signed-off-by: Christoph Knittel <ck@cca.io>
Publication only touches the module's own files; build-state updates stay on the scheduler domain.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72
Signed-off-by: Christoph Knittel <ck@cca.io>
The default heap made workers stop each other for minor collections. Clean builds of tests/tests take about 20% less wall time and 27% less CPU, for about 200 MB more memory at 8 workers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72
Signed-off-by: Christoph Knittel <ck@cca.io>
Process_child's payload was always unit, Btype.reinit never rewound a fresh request's counter, and the bsc test proxy only needs its counting mode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72
Signed-off-by: Christoph Knittel <ck@cca.io>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72
Signed-off-by: Christoph Knittel <ck@cca.io>
compiler-info.json named a bsc.exe next to the executable even where there is none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72
Signed-off-by: Christoph Knittel <ck@cca.io>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72
Signed-off-by: Christoph Knittel <ck@cca.io>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the rewatch-ocaml-in-process branch from d2d1c04 to de1bf35 Compare October 14, 2026 19:46
@cknitt
cknitt marked this pull request as ready for review October 15, 2026 04:16
@cknitt
cknitt requested a review from cristianoc October 15, 2026 04:16

@cristianoc cristianoc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where I would have expected a detectable per difference.
Or difference in allocation profile.

But perhaps I don't understand the spirit of what is done here.

cknitt and others added 2 commits October 15, 2026 04:58
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72
Signed-off-by: Christoph Knittel <ck@cca.io>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt

cknitt commented Oct 15, 2026

Copy link
Copy Markdown
Member Author

@cristianoc There is now a "Performance" section in the PR description.

@cknitt
cknitt merged commit f732de2 into master Oct 15, 2026
26 checks passed
@cknitt
cknitt deleted the rewatch-ocaml-in-process branch October 15, 2026 15:46
@cknitt cknitt mentioned this pull request Oct 15, 2026

Copy link
Copy Markdown
Collaborator

That’s great, thanks for the benchmarks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants